package com.snbc.zookeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @author shichenguang
 * @desciption 描述
 */
public class ZookeeperUtil {
    private ZooKeeper zooKeeper;

    public ZookeeperUtil(Watcher watcher) {
        try {
            String connString = "snbc:2182,snbc:2183,snbc:2184";
            zooKeeper = new ZooKeeper(connString, 5000, watcher);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public ZooKeeper getZK(){
        return zooKeeper;
    }

    public void setZooKeeper(ZooKeeper zooKeeper) {
        this.zooKeeper = zooKeeper;
    }

    public ZooKeeper getZooKeeper() {

        return zooKeeper;
    }


    /**
     * 创建znode结点
     * @param path 结点路径
     * @param data 结点数据
     * @return true 创建结点成功 false表示结点存在
     * @throws Exception
     */
    public boolean addZnodeData(String path, String data, CreateMode mode){
        try {
            if(zooKeeper.exists(path, true) == null){
                zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
                return true;
            }
        } catch (KeeperException | InterruptedException e) {
            throw new RuntimeException("创建znode："+path+"出现问题！！",e);
        }
        System.out.println("znode"+path+"结点已存在");
        return false;
    }


    /**
     * 创建永久性节点
     * @param path
     * @param data
     * @return
     */
    public boolean addPersistentNode(String path,String data){
        return addZnodeData(path,data,CreateMode.PERSISTENT);
    }

    /**
     * 创建临时节点
     * @param path
     * @param data
     * @return
     */
    public boolean addEphemeralNode(String path,String data){
        return addZnodeData(path,data,CreateMode.EPHEMERAL);
    }


    /**
     * 修改znode
     * @param path 节点路径
     * @param data 节点数据
     * @return  true 修改节点成功 false 节点不存在
     */
    public boolean updataZnode(String path, String data){
        try {
            Stat state = zooKeeper.exists(path, true);
            if (state != null){
                zooKeeper.setData(path, data.getBytes(), state.getVersion());
                return  true;
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除节点
     * @param path
     * @return
     */
    public boolean deleteZnode(String path){
        try {
            Stat state = zooKeeper.exists(path, true);
            if(state != null){
                List<String> children = zooKeeper.getChildren(path, false);
                if(children.isEmpty()){
                    zooKeeper.delete(path,state.getVersion());
                    return true;
                }else {
                    children.forEach(itemPath->deleteZnode(path+"/"+itemPath));
                }
            }

        }catch (Exception e){
            e.printStackTrace();

        }
        return false;
    }


    /**
     * 获取数据
     * @param path
     * @return
     */
    public String getZnodeData(String path){
        String resultData = "";
        try {
            Stat state = zooKeeper.exists(path, true);
            if(state != null){
                resultData =new String(zooKeeper.getData(path,true,state));
            }else {
                System.out.println("znode:");
            }
            return resultData;
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultData;
    }
}
